package com.frogs.ucenter.service.impl.scheduledTasks;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.util.PropertyUtil;
import com.frogs.framework.util.ResourceUtils;

@Service("updateJsTask")
public class UpdateJsTask {
	
	private static Logger log = LoggerFactory.getLogger(UpdateJsTask.class);
	
	private static String propertiesFileName = "jsVersion";
	
	private static Map<String, String> jsVersion = new HashMap<String, String>();
		
	public static Map<String, String> getMap(){
		return jsVersion;
	}
	
	/**
	 * 初始化js和css的版本，此方法有两个调用的地方，一个是系统初始化的时候，另一个是定时调用
	 */
	public void initJsVersion(){
		String mypath = PropertyUtil.getProperty(propertiesFileName, "filePath");
		mypath = Logger.class.getResource(mypath).toString();
		String laststr = "";
		FileInputStream fileInputStream = null;
		BufferedReader reader = null;
		try {
			File file = ResourceUtils.getFile(mypath);
			fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
			reader = new BufferedReader(inputStreamReader);
			String tempString = "";
			while((tempString = reader.readLine()) != null){
				laststr += tempString;
			}
			JSONObject jsonObject = (JSONObject) JSONObject.parse(laststr);
			JSONObject json = (JSONObject) jsonObject.get("res");
			Set<String> set = json.keySet();
			Iterator<String> iterator = set.iterator();
		    while(iterator.hasNext()){//遍历  
		    	String key = iterator.next().toString();
		    	JSONObject jsonObject1 = (JSONObject) JSONObject.parse(json.getString(key));
		    	jsVersion.put(key, jsonObject1.getString("uri"));
		    } 
		    reader.close();
		    fileInputStream.close();
		} catch (FileNotFoundException e) {
			log.error("func[init FileNotFoundException] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { "", "" , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		} catch (UnsupportedEncodingException e) {
			log.error("func[init UnsupportedEncodingException] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { "", "" , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		} catch (IOException e) {
			log.error("func[init IOException] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { "", "" , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}finally{
			if(fileInputStream != null){
				try {
					fileInputStream.close();
				} catch (IOException e) {
					log.error("func[init fileInputStream IOException] url[{}] params[{}] exception[{} - {}] desc[fail]",
							new Object[] { "", "" , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
				}
			}
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					log.error("func[init reader IOException] url[{}] params[{}] exception[{} - {}] desc[fail]",
							new Object[] { "", "" , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
				}
			}
		}
	}
}
